package runaservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import runaservice.UserInfoManagement;

/**
 * Servlet implementation class UserInfoManagementServlet
 */
@WebServlet("/UserInfoManagementServlet")
public class UserInfoManagementServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserInfoManagementServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter writer = response.getWriter();
		try{
			String methodString = request.getParameter("method");
			switch (methodString) {
			case "register":
			{
				String sex = request.getParameter("sex");
				String birth = request.getParameter("birth");
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				System.out.println("sex="+sex+"birth="+birth);
				new UserInfoManagement().register(sex, birth, username,password, writer);
				break;
			}

			case "login":
			{
				String ID=request.getParameter("ID");
				String password = request.getParameter("password");
				new UserInfoManagement().login(ID, password, writer);
				break;
			}
			
			case "exit":
			{
				String ID = request.getParameter("ID");
				new UserInfoManagement().exit(ID, writer);
				break;
			}
			default:
			{
				break;
			}
			
			}
		}catch(Exception exception)
		{
			writer.println("errorcode=1&errormessage="+exception.getMessage());
		}
		writer.flush();writer.close();
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
